﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;


namespace 功能测试项目
{
    

    /// <summary>
    /// ModBusStatus读写控制状态都在这里
    /// </summary>
    public enum ModBusStatus
    {
        /// <summary>
        /// 没有任何操作
        /// </summary>
        Free,
        /// <summary>
        /// 获取模块类型
        /// </summary>
        GetMoudleType,
        /// <summary>
        /// 获取温湿度值
        /// </summary>
        GetTH,
        /// <summary>
        /// 获取LED参数
        /// </summary>
        GetLEDParam,
        /// <summary>
        /// 设置LED参数
        /// </summary>
        SetLEDParam,
        /// <summary>
        /// 获取温湿度偏移量
        /// </summary>
        GetTHOffset,
        /// <summary>
        /// 设置温湿度偏移量
        /// </summary>
        SetTHOffset,
        /// <summary>
        /// 获取温湿度数据更新时间
        /// </summary>
        GetTHDataUpDateTime,
        /// <summary>
        /// 设置温湿度数据更新时间
        /// </summary>
        SetTHDataUpDateTime,
        /// <summary>
        /// 获取基本参数
        /// </summary>
        GetBasicParam,
        /// <summary>
        /// 设置基本参数
        /// </summary>
        SetBasicParam,
        /// <summary>
        /// 获取自动上报参数
        /// </summary>
        GetAutoUpParam,
        /// <summary>
        /// 设置自动上报参数
        /// </summary>
        SetAutoUpParam,
        /// <summary>
        /// 获取报警参数
        /// </summary>
        GetWarningParam,
        /// <summary>
        /// 设置报警参数
        /// </summary>
        SetWarningParam,


        /// <summary>
        /// 输出out接收通
        /// </summary>
        WRITE_DO1_ON,
        /// <summary>
        /// 输出out断开
        /// </summary>
        WRITE_DO1_OFF,
        /// <summary>
        /// 写设备ID
        /// </summary>
        WRITE_ID,

        

    }

    /// <summary>
    /// ModBusRTU类
    /// </summary>
    public class CModBusRTU
    {
        public static byte MID = 1;

        static byte[] aucCRCHi = {
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
            0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
            0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
            0x00, 0xC1, 0x81, 0x40
        };

        static byte[] aucCRCLo = {
            0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7,
            0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E,
            0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9,
            0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC,
            0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
            0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32,
            0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D,
            0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38,
            0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF,
            0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
            0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1,
            0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4,
            0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB,
            0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA,
            0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
            0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0,
            0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97,
            0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E,
            0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89,
            0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
            0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83,
            0x41, 0x81, 0x80, 0x40
        };

        //---------------------------------------------------------------------------
        //CRC16 校验代码
        public static UInt16 CRC16(byte[] pucFrame, byte usLen)
        {
            byte ucCRCHi = 0xFF;
            byte ucCRCLo = 0xFF;
            byte len = 0;
            UInt16 iIndex;

            while ((len) < usLen)
            {

                iIndex = (UInt16)(ucCRCLo ^ pucFrame[len]);
                len++;
                ucCRCLo = (byte)(ucCRCHi ^ aucCRCHi[iIndex]);
                ucCRCHi = aucCRCLo[iIndex];
            }
            return (UInt16)(ucCRCHi << 8 | ucCRCLo);
        }

        /// <summary>
        /// 读函数
        /// </summary>
        /// <param name="id">从机id</param>
        /// <param name="FunCode">功能码</param>
        /// <param name="RegAddr">寄存器起始地址</param>
        /// <param name="RegNum">要读的寄存器数量</param>
        /// <returns></returns>
        public static byte[] ModBusRTU_Read(
                                            byte FunCode,
                                            UInt16 RegAddr,
                                            UInt16 RegNum)
        {
            byte[] Buffer = {  0x01,
                                        0x03,       //功能码
                                        0x00,0x00,  //寄存器地址
                                        0x00,0x00,  //寄存器数量
                                        0x00,0x00   //CRC检验字节
                                        };
            UInt16 CRCVal = 0;

            Buffer[0] = MID;

            Buffer[1] = FunCode;

            Buffer[2] = (byte)(RegAddr >> 8);
            Buffer[3] = (byte)RegAddr;

            Buffer[4] = (byte)(RegNum >> 8);
            Buffer[5] = (byte)RegNum;

            CRCVal = CRC16(Buffer,6);

            Buffer[6] = (byte)CRCVal;
            Buffer[7] = (byte)(CRCVal >> 8);

            return Buffer;
        }

        /// <summary>
        /// Modbus写函数
        /// </summary>
        /// <param name="id">设备id</param>
        /// <param name="ParamFlag">写参数标志</param>
        /// <param name="buf">参数数据，默认为空</param>
        public static byte[] ModBusRTU_Write(
            byte FunCode,
            UInt16 RegNum,
            UInt16 RegCnt,
            ModBusStatus ParamFlag,
            byte[] buf=null)
        {
            byte[] Buffer = {0x01,
                            0x03,       //功能码
                            0x00,0x00,  //寄存器地址
                            0x00,0x00,  //寄存器数量
                            0x00,0x00,
                            0x00,0x00,
                            0x00,0x00,
                            0x00,0x00,
                            0x00,0x00,
                            0x00,0x00,
                            0x00,0x00   //CRC检验字节
                            };

            uint CRCVal = 0;
            uint SendNum = 0;

            //id
            Buffer[0] = MID;

            switch (ParamFlag)
            {
                case ModBusStatus.WRITE_DO1_ON:
                    //Buffer[1] = FunCode;

                    //Buffer[2] = (byte)(RegNum>>8);
                    //Buffer[3] = (byte)RegNum;

                    //Buffer[4] = buf[0];
                    //Buffer[5] = buf[1];

                    //SendNum = 8;
                    //break;

                case ModBusStatus.WRITE_DO1_OFF:
                    Buffer[1] = FunCode;

                    Buffer[2] = (byte)(RegNum>>8);
                    Buffer[3] = (byte)RegNum;

                    Buffer[4] = buf[0];
                    Buffer[5] = buf[1];

                    SendNum = 8;
                    break;

                /// <summary>
                /// 设置LED参数
                /// </summary>
                case ModBusStatus.SetLEDParam:
                    //break;
                /// <summary>
                /// 设置温湿度偏移量
                /// </summary>
                case ModBusStatus.SetTHOffset:
                    //break;
                /// <summary>
                /// 设置温湿度数据更新时间
                /// </summary>
                case ModBusStatus.SetTHDataUpDateTime:
                    //break;
                /// <summary>
                /// 设置基本参数
                /// </summary>
                case ModBusStatus.SetBasicParam:
                    //break;
                /// <summary>
                /// 设置自动上报参数
                /// </summary>
                case ModBusStatus.SetAutoUpParam:
                    //break;
                /// <summary>
                /// 设置报警参数
                /// </summary>
                case ModBusStatus.SetWarningParam:
                //break;
                //case ModBusStatus.WRITE_DO1_OFF:
                ////break;
                //case ModBusStatus.WRITE_DO1_ON:
                //    //break;
                case ModBusStatus.WRITE_ID:
                    //Buffer[1] = 0x10;

                    //Buffer[2] = (byte)(0x00);//寄存器号
                    //Buffer[3] = (byte)0x00;

                    //Buffer[4] = (byte)(0x00);//寄存器个数
                    //Buffer[5] = (byte)0x03;

                    //Buffer[6] = (byte)(Buffer[5] * 2);  //数据长度

                    ////Buffer[7] = (byte)(SetId >> 8);
                    ////Buffer[8] = (byte)SetId;

                    ////Buffer[9] = (byte)(SetBaud >> 8);
                    ////Buffer[10] = (byte)SetBaud;

                    ////Buffer[11] = (byte)(UnionMove >> 8);
                    ////Buffer[12] = (byte)UnionMove;

                    //SendNum = (byte)(7 + Buffer[6] + 2);
                    Buffer[1] = FunCode;

                    Buffer[2] = (byte)(RegNum >> 8);//寄存器号
                    Buffer[3] = (byte)RegNum;

                    Buffer[4] = (byte)(RegCnt >> 8);//寄存器个数
                    Buffer[5] = (byte)RegCnt;

                    Buffer[6] = (byte)(buf.Length);// (byte)(Buffer[5] * 2);  //数据长度

                    for (int i = 0; i < buf.Length; i++)
                    {
                        Buffer[7 + i] = buf[i];
                    }

                    SendNum = (byte)(7 + Buffer[6] + 2);
                    break;


                default:

                    break;
            }

            CRCVal = CRC16(Buffer, (byte)(SendNum - 2));

            Buffer[SendNum - 2] = (byte)CRCVal;
            Buffer[SendNum - 1] = (byte)(CRCVal >> 8);
            byte[] bufArr = new byte[SendNum];
            Array.Copy(Buffer, bufArr, SendNum);

            return bufArr;
        }

        

    }



    public class ConvertFun
    {

        static byte[] CRC1 =
        {
              0x07,0x96,0xe4,0x75,0x00,0x91,0xe3,0x72,//00-07
              0x09,0x98,0xea,0x7b,0x0e,0x9f,0xed,0x7c,//08-0f
              0x1b,0x8a,0xf8,0x69,0x1c,0x8d,0xff,0x6e,//10-17
              0x15,0x84,0xf6,0x67,0x12,0x83,0xf1,0x60,//18-1f
              0x3f,0xae,0xdc,0x4d,0x38,0xa9,0xdb,0x4a,//20-27
              0x31,0xa0,0xd2,0x43,0x36,0xa7,0xd5,0x44,//28-2f
              0x23,0xb2,0xc0,0x51,0x24,0xb5,0xc7,0x56,//30-37
              0x2d,0xbc,0xce,0x5f,0x2a,0xbb,0xc9,0x58,//38-3f
              0x77,0xe6,0x94,0x05,0x70,0xe1,0x93,0x02,//40-47
              0x79,0xe8,0x9a,0x0b,0x7e,0xef,0x9d,0x0c,//48-4f
              0x6b,0xfa,0x88,0x19,0x6c,0xfd,0x8f,0x1e,//50-57
              0x65,0xf4,0x86,0x17,0x62,0xf3,0x81,0x10,//58-5f
              0x4f,0xde,0xac,0x3d,0x48,0xd9,0xab,0x3a,//60-67
              0x41,0xd0,0xa2,0x33,0x46,0xd7,0xa5,0x34,//68-6f
              0x53,0xc2,0xb0,0x21,0x54,0xc5,0xb7,0x26,//70-77
              0x5d,0xcc,0xbe,0x2f,0x5a,0xcb,0xb9,0x28,//78-7f
              0xe7,0x76,0x04,0x95,0xe0,0x71,0x03,0x92,//80-87
              0xe9,0x78,0x0a,0x9b,0xee,0x7f,0x0d,0x9c,//88-8f
              0xfb,0x6a,0x18,0x89,0xfc,0x6d,0x1f,0x8e,//90-97
              0xf5,0x64,0x16,0x87,0xf2,0x63,0x11,0x80,//98-9f
              0xdf,0x4e,0x3c,0xad,0xd8,0x49,0x3b,0xaa,//a0-a7
              0xd1,0x40,0x32,0xa3,0xd6,0x47,0x35,0xa4,//a8-af
              0xc3,0x52,0x20,0xb1,0xc4,0x55,0x27,0xb6,//b0-b7
              0xcd,0x5c,0x2e,0xbf,0xca,0x5b,0x29,0xb8,//b8-bf
              0x97,0x06,0x74,0xe5,0x90,0x01,0x73,0xe2,//c0-c7
              0x99,0x08,0x7a,0xeb,0x9e,0x0f,0x7d,0xec,//c8-cf
              0x8b,0x1a,0x68,0xf9,0x8c,0x1d,0x6f,0xfe,//d0-d7
              0x85,0x14,0x66,0xf7,0x82,0x13,0x61,0xf0,//d8-df
              0xaf,0x3e,0x4c,0xdd,0xa8,0x39,0x4b,0xda,//e0-e7
              0xa1,0x30,0x42,0xd3,0xa6,0x37,0x45,0xd4,//e8-ef
              0xb3,0x22,0x50,0xc1,0xb4,0x25,0x57,0xc6,//f0-f7
              0xbd,0x2c,0x5e,0xcf,0xba,0x2b,0x59,0xc8,//f8-ff
        };

        static byte[] CRC2 =
        {
              0x47,0xd6,0x35,0xd1,0xa4,0x40,0xa3,0x32,//a8-af
              0x49,0xd8,0x3b,0xdf,0xaa,0x4e,0xad,0x3c,//a0-a7
              0x55,0xc4,0x27,0xc3,0xb6,0x52,0xb1,0x20,//b0-b7
              0x71,0xe0,0x03,0xe7,0x92,0x76,0x95,0x04,//80-87
              0x5b,0xca,0x29,0xcd,0xb8,0x5c,0xbf,0x2e,//b8-bf
              0x7f,0xee,0x0d,0xe9,0x9c,0x78,0x9b,0x0a,//88-8f
              0x6d,0xfc,0x1f,0xfb,0x8e,0x6a,0x89,0x18,//90-97
              0x39,0xa8,0x4b,0xaf,0xda,0x3e,0xdd,0x4c,//e0-e7
              0x63,0xf2,0x11,0xf5,0x80,0x64,0x87,0x16,//98-9f
              0x37,0xa6,0x45,0xa1,0xd4,0x30,0xd3,0x42,//e8-ef
              0x2b,0xba,0x59,0xbd,0xc8,0x2c,0xcf,0x5e,//f8-ff
              0x25,0xb4,0x57,0xb3,0xc6,0x22,0xc1,0x50,//f0-f7
              0x01,0x90,0x73,0x97,0xe2,0x06,0xe5,0x74,//c0-c7
              0x1d,0x8c,0x6f,0x8b,0xfe,0x1a,0xf9,0x68,//d0-d7
              0x0f,0x9e,0x7d,0x99,0xec,0x08,0xeb,0x7a,//c8-cf
              0x13,0x82,0x61,0x85,0xf0,0x14,0xf7,0x66,//d8-df
              0x83,0x12,0xf1,0x15,0x60,0x84,0x67,0xf6,//18-1f
              0x8d,0x1c,0xff,0x1b,0x6e,0x8a,0x69,0xf8,//10-17
              0x91,0x00,0xe3,0x07,0x72,0x96,0x75,0xe4,//00-07
              0xa9,0x38,0xdb,0x3f,0x4a,0xae,0x4d,0xdc,//20-27
              0x9f,0x0e,0xed,0x09,0x7c,0x98,0x7b,0xea,//08-0f
              0xa7,0x36,0xd5,0x31,0x44,0xa0,0x43,0xd2,//28-2f
              0xef,0x7e,0x9d,0x79,0x0c,0xe8,0x0b,0x9a,//48-4f
              0xe1,0x70,0x93,0x77,0x02,0xe6,0x05,0x94,//40-47
              0xb5,0x24,0xc7,0x23,0x56,0xb2,0x51,0xc0,//30-37
              0xbb,0x2a,0xc9,0x2d,0x58,0xbc,0x5f,0xce,//38-3f
              0xf3,0x62,0x81,0x65,0x10,0xf4,0x17,0x86,//58-5f
              0xfd,0x6c,0x8f,0x6b,0x1e,0xfa,0x19,0x88,//50-57
              0xc5,0x54,0xb7,0x53,0x26,0xc2,0x21,0xb0,//70-77
              0xd9,0x48,0xab,0x4f,0x3a,0xde,0x3d,0xac,//60-67
              0xcb,0x5a,0xb9,0x5d,0x28,0xcc,0x2f,0xbe,//78-7f
              0xd7,0x46,0xa5,0x41,0x34,0xd0,0x33,0xa2//68-6f
        };

        public static byte buffer_crc1(byte st, byte len, byte[] buffer)
        {
            byte i, crc;
            crc = 0xaa;
            for (i = 0; i < len; i++)
            {
                crc = CRC1[crc ^ buffer[i + st]];
            }
            return crc;
        }
        public static byte buffer_crc2(byte st, byte len, byte[] buffer)
        {
            byte i, crc;
            crc = 0xaa;
            for (i = 0; i < len; i++) crc = CRC2[crc ^ buffer[i + st]];
            return crc;
        }

        //[DllImport("CC200.dll")]
        //public static extern byte buffer_crc1(byte st, byte len, byte[] buf);
        //[DllImport("CC200.dll")]
        //public static extern byte buffer_crc2(byte st, byte len, byte[] buf);
        /// <summary>
        /// 字符串转16进制字节数组
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static byte[] strToToHexByte(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }
        /// <summary>
        /// 十六进制字符串转为ASCII码字符
        /// 例如:"3132333435"
        /// 转为:"12345"
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static string HexstrToASCIIstr(string hexString)
        {
            byte[] bArr = strToToHexByte(hexString);
            string str = System.Text.Encoding.UTF8.GetString(bArr);
            return str;
        }

        /// <summary>
        /// 字节数组转16进制字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns>返回一个16进制字符串</returns>
        public static string byteToHexStr(byte[] bytes)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("X2");
                }
            }
            return returnStr;
        }

        /// <summary>
        /// 字节数组转16进制字符串，每个字节以空格分隔
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns>返回一个16进制字符串，每个字节间以空格分隔</returns>
        public static string byteToHexStrEmty(byte[] bytes)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("X2");
                    if (i < (bytes.Length - 1))
                        returnStr += " ";
                }
            }
            return returnStr;
        }

        /// <summary>
        /// 从汉字转换到16进制
        /// </summary>
        /// <param name="s"></param>
        /// <param name="charset">编码,如"utf-8","gb2312"</param>
        /// <param name="fenge">是否每字符用逗号分隔</param>
        /// <returns></returns>
        public static string ToHex(string s, string charset, bool fenge)
        {
            if ((s.Length % 2) != 0)
            {
                s += " ";//空格
                         //throw new ArgumentException("s is not valid chinese string!");
            }
            System.Text.Encoding chs = System.Text.Encoding.GetEncoding(charset);
            byte[] bytes = chs.GetBytes(s);
            string str = "";
            for (int i = 0; i < bytes.Length; i++)
            {
                str += string.Format("{0:X}", bytes[i]);
                if (fenge && (i != bytes.Length - 1))
                {
                    str += string.Format("{0}", ",");
                }
            }
            return str.ToLower();
        }

        ///<summary>
        /// 从16进制转换成汉字
        /// </summary>
        /// <param name="hex"></param>
        /// <param name="charset">编码,如"utf-8","gb2312"</param>
        /// <returns></returns>
        public static string UnHex(string hex, string charset)
        {
            if (hex == null)
                throw new ArgumentNullException("hex");
            hex = hex.Replace(",", "");
            hex = hex.Replace("\n", "");
            hex = hex.Replace("\\", "");
            hex = hex.Replace(" ", "");
            if (hex.Length % 2 != 0)
            {
                hex += "20";//空格
            }
            // 需要将 hex 转换成 byte 数组。 
            byte[] bytes = new byte[hex.Length / 2];

            for (int i = 0; i < bytes.Length; i++)
            {
                try
                {
                    // 每两个字符是一个 byte。 
                    bytes[i] = byte.Parse(hex.Substring(i * 2, 2),
                    System.Globalization.NumberStyles.HexNumber);
                }
                catch
                {
                    // Rethrow an exception with custom message. 
                    throw new ArgumentException("hex is not a valid hex number!", "hex");
                }
            }
            System.Text.Encoding chs = System.Text.Encoding.GetEncoding(charset);
            return chs.GetString(bytes);
        }
    }


    /// <summary>
    /// 控制类
    /// </summary>
    public class ControlClass
    {
        /// <summary>
        /// 根据连接打开网页
        /// </summary>
        /// <param name="url"></param>
        static public void OpenLink(string url)
        {
            System.Diagnostics.Process.Start(url);
        }
    }

}
